<?php
/**
 *
 * @Author nikm $
 *
 * @Date 2012-04-01 $
 */

if(!defined('FROM_INDEX')) exit('Invalid request');

require ROOT_PATH.'module/master.mod.php';
require_once ROOT_PATH.'include/lib/image.func.php';

class moduleObj extends masterObj
{
	function __construct($request)
	{
		parent::__construct($request);
		
		$this->excute();
	}
	
	private function excute()
	{
		ob_start();
		
		switch($this->act)
		{
			case 'add':
				$this->add();
				break;
			case 'do_add':
				$this->doAdd();
				break;
			case 'do_delete':
				$this->doDelete();
				break;
			case 'modify':
				$this->modify();
				break;
			case 'do_modify':
				$this->doModify();
				break;
			case 'type':
				$this->getTypes();
				break;
			case 'add_type':
				$this->addType();
				break;
			case 'del_type':
				$this->deleteType();
				break;
			default:
				$this->main();
				break;
		}
		
		$body = ob_get_clean();
		
		$this->showBody($body);
	}
	
	private function main()
	{
		$list = array();
		
		if(isset($this->user['item_list']) && check_array($this->user['item_list']))
		{
			$item_list = $this->user['item_list'];
			
			$ids = array();
			foreach($item_list as $item)
			{
				if($item['type'] == 'ho')
				{
					$ids[] = $item['item_id'];
				}
			}
			
			if(count($ids) > 0)
			{
				$this->request['ids'] = implode(',', $ids);
				
				$result = get_hostel_list($this->request);
				$list = $result['result'];
			}
		}
		
		$page_html = '';
		
		include template('hostel');
	}
	
	private function add()
	{
		$info = array();
		if(isset($this->request['c']))
		{
			$info = cache_get($this->request['c']);
			cache_delete($this->request['c']);
		}
		
		$info['name'] = isset($info['name']) ? $info['name'] : '';
		$info['image'] = isset($info['image']) ? $info['image'] : '';
		$info['address'] = isset($info['address']) ? $info['address'] : '';
		$info['telephone'] = isset($info['telephone']) ? $info['telephone'] : '';
		
		$info['legal_person'] = isset($info['legal_person']) ? $info['legal_person'] : '';
		$info['number'] = isset($info['number']) ? $info['number'] : '';
		
		$info['star_level'] = isset($info['star_level']) ? $info['star_level'] : '';
		$info['min_price'] = isset($info['min_price']) ? $info['min_price'] : '';
		$info['service_score'] = isset($info['service_score']) ? $info['service_score'] : '';
		$info['env_score'] = isset($info['env_score']) ? $info['env_score'] : '';
		
		$info['customer_score'] = '';
		$info['desc'] = isset($info['desc']) ? $info['desc'] : '';
		$info['notice'] = isset($info['notice']) ? $info['notice'] : '';
		$info['traffic'] = isset($info['traffic']) ? $info['traffic'] : '';		
		
		$info['longitude'] = isset($info['longitude']) ? $info['longitude'] : '';
		$info['latitude'] = isset($info['latitude']) ? $info['latitude'] : '';
		
		$action = 'do_add';
		$action_name = '添加酒店';
		include template('hostel_info');
	}
	
	private function doAdd()
	{
		//DBFunction::getInstance()->setDebug();
		
		$info = $this->_check_hostel_info();
		$info['uid'] = $this->user['id'];
		
		if($_FILES['image']['name'])
		{
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$info['image'] = $image_path;
			}
		}
		
		$result = add_hostel($info);
		
		if($result)
		{
			$user = $_SESSION['user'];
			$user['item_list'][] = array('item_id'=>$result, 'type'=>'ho');
			$_SESSION['user'] = $user;
			
			add_user_travel_item(array('uid'=>$info['uid'], 'item_id'=>$result, 'type'=>'ho'));
			
			$this->messager("添加成功", 'index.php?m=hostel&a=modify&id='.$result);
		}
		else
		{
			$this->messager("添加失败", 'index.php?m=hostel&a=add');
		}
	}
	
	private function modify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$hid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$hid)
		{
			$this->messager("请指定ID", 'index.php?m=hostel');
		}
		
		if(isset($this->request['c']) && ($info = cache_get($this->request['c'])) !== false)
		{
			cache_delete($this->request['c']);
		}
		else
		{
			$info = get_hostel_info(array('hid'=>$hid));
			
			if(!isset($info['id']) || $info['id'] < 1)
			{
				$this->messager("酒店不存在", 'index.php?m=hostel');
			}
		}
				
		$action = 'do_modify&id='.$hid;
		$action_name = '编辑酒店';
		include template('hostel_info');
	}
	
	private function doModify()
	{
		//DBFunction::getInstance()->setDebug();
		
		$hid = isset($this->request['id']) ? (int)$this->request['id'] : '';
		
		if(!$hid)
		{
			$this->messager("请指定ID", 'index.php?m=hostel');
		}
		
		$hostel = get_hostel_info(array('hid'=>$hid));
		
		if(!isset($hostel['id']) || $hostel['id'] < 1)
		{
			$this->messager("酒店不存在", 'index.php?m=hostel');
		}
		
		$info = $this->_check_hostel_info();
		$info['uid'] = $this->user['id'];

		if($_FILES['image']['name'])
		{
			$old_logo = $hostel['image'];
			
			if(is_file($old_logo))
			{
				delete_image($old_logo);
			}
			
			$image_dir = 'data/images/'.rand(1, 99).'/';
			$image_path = move_image_to_path('image', $image_dir);
			if($image_path)
			{
				$info['image'] = $image_path;
			}
		}
		else
		{
			$info['image'] = $hostel['image'];
		}
		
		$result = update_hostel($info);
		
		if($result)
		{
			$this->messager("修改成功", 'index.php?m=hostel');
		}
		else
		{
			$this->messager("修改失败", 'index.php?m=hostel');
		}
	}
	
	private function doDelete()
	{
		$id = isset($this->request['id']) ? $this->request['id'] : '';
		
		if(!$id)
		{
			$this->messager("请指定ID", 'index.php?m=hostel');
		}
		
		$result = delete_hostel(array('id'=>$id));
				
		if($result)
		{
			delete_user_travel_item(array('uid'=>$this->user['uid'], 'item_id'=>$id));
			
			$this->messager("删除成功", 'index.php?m=hostel');
		}
		else
		{
			$this->messager("删除失败", 'index.php?m=hostel');
		}
	}
	
	private function _check_hostel_info()
	{
		$info['id'] = isset($this->request['id']) ? $this->request['id'] : '';
		
		$info['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		$info['image'] = '';
		$info['address'] = isset($this->request['address']) ? $this->request['address'] : '';
		$info['telephone'] = isset($this->request['telephone']) ? $this->request['telephone'] : '';
		
		$info['legal_person'] = isset($this->request['legal_person']) ? $this->request['legal_person'] : '';
		$info['number'] = isset($this->request['number']) ? $this->request['number'] : '';
		
		$info['star_level'] = isset($this->request['star_level']) ? (int)$this->request['star_level'] : '';
		$info['min_price'] = '0';
		$info['service_score'] = '0';
		$info['env_score'] = '0';
		
		$info['desc'] = isset($this->request['desc']) ? $this->request['desc'] : '';
		$info['notice'] = isset($this->request['notice']) ? $this->request['notice'] : '';
		$info['traffic'] = isset($this->request['traffic']) ? $this->request['traffic'] : '';
		
		$info['longitude'] = isset($this->request['longitude']) ? (float)$this->request['longitude'] : '';
		$info['latitude'] = isset($this->request['latitude']) ? (float)$this->request['latitude'] : '';
				
		$key = md5(implode(',', $info));
		cache_set($key, $info);
		
		$redirect = "index.php?m=hostel&a=add&c=$key";
				
		$message = '';
		if(!$info['name'] || !$info['address'] || !$info['telephone'])
        {
			$this->messager("内容不能为空", $redirect);
		}
		if(strlen($info['name']) > 250)
		{
			$this->messager("名称超长", $redirect);
		}
		if(strlen($info['telephone']) > 250)
		{
			$this->messager("电话超长", $redirect);
		}
		if(strlen($info['desc']) > 2999)
		{
			//$this->messager("简介超长", $redirect);
			$info['desc'] = cut_string($info['desc'], 2995);
		}
		if(strlen($info['traffic']) > 2999)
		{
			//$this->messager("简介超长", $redirect);
			$info['traffic'] = cut_string($info['traffic'], 2995);
		}
		if(strlen($info['address']) > 250)
		{
			$this->messager("地址超长", $redirect);
		}
		
		if(!$info['id'])
		{
			$is_exists = check_travel_item_exists($info['name'], $info['id']);
			
			if($is_exists)
			{
				$this->messager("酒店名称 《{$info['name']}》 已存在", 'index.php?m=hostel');
			}
		}
				
		return $info;
	}
	
	private function getTypes()
	{
		//DBFunction::getInstance()->setDebug();
		
		$result = get_room_types();
		
		$list = $result;
		
		include template('hostel_room_type');
	}
	
	private function addType()
	{
		$info['name'] = isset($this->request['name']) ? $this->request['name'] : '';
		
		$result = add_room_type($info);
		
		if($result)
		{
			$this->messager("添加成功", 'index.php?m=hostel&a=type');
		}
		else
		{
			$this->messager("添加失败", 'index.php?m=hostel&a=type');
		}
	}
	
	private function deleteType()
	{
		$ids = isset($this->request['ids']) ? $this->request['ids'] : '';
		
		$result = '';
		if(stripos($ids, ','))
		{
			$ids = explode(',', $ids);
			
			foreach($ids as $id)
			{
				$result = delete_room_type($id);
			}
		}
		else
		{
			$result = delete_room_type($ids);
		}
		
		if($result)
		{
			$this->messager("删除成功", 'index.php?m=hostel&a=type');
		}
		else
		{
			$this->messager("删除失败", 'index.php?m=hostel&a=type');
		}
	}
}